package com.pristine.util;

import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

import org.apache.commons.collections.CollectionUtils;

import com.pristine.domain.CategoryTypeMasterEntity;
import com.pristine.domain.ConsumableEntity;
import com.pristine.domain.CountryMasterEntity;
import com.pristine.domain.CustomerEntity;
import com.pristine.domain.DepartmentMasterEntity;
import com.pristine.domain.DoctorMasterEntity;
import com.pristine.domain.EmployeeEntity;
import com.pristine.domain.EquipmentEntity;
import com.pristine.domain.MedicineEntity;
import com.pristine.domain.OperatorEntity;
import com.pristine.domain.PatientEntity;
import com.pristine.domain.PhoneNumberEntity;
import com.pristine.domain.StateMasterEntity;
import com.pristine.domain.TreatmentPackageEntity;
import com.pristine.service.wsdl.CategoryTypeMasterVO;
import com.pristine.service.wsdl.ConsumableVO;
import com.pristine.service.wsdl.CountryMasterVO;
import com.pristine.service.wsdl.CustomerVO;
import com.pristine.service.wsdl.DepartmentMasterVO;
import com.pristine.service.wsdl.DoctorMasterVO;
import com.pristine.service.wsdl.EmployeeVO;
import com.pristine.service.wsdl.EquipmentVO;
import com.pristine.service.wsdl.MedicineVO;
import com.pristine.service.wsdl.OperatorVO;
import com.pristine.service.wsdl.PatientVO;
import com.pristine.service.wsdl.PhoneNumberVO;
import com.pristine.service.wsdl.StateMasterVO;
import com.pristine.service.wsdl.TreatmentPackageVO;

public class EntityToVOConverter {
	/*
	 * Converts java.util.Date to javax.xml.datatype.XMLGregorianCalendar
	 */
	public static XMLGregorianCalendar toXMLGregorianCalendar(Date date) {
		GregorianCalendar gCalendar = new GregorianCalendar();
		gCalendar.setTime(date);
		XMLGregorianCalendar xmlCalendar = null;
		try {
			xmlCalendar = DatatypeFactory.newInstance()
					.newXMLGregorianCalendar(gCalendar);
		} catch (DatatypeConfigurationException ex) {
		}
		return xmlCalendar;
	}

	public static CategoryTypeMasterVO convert(CategoryTypeMasterEntity entity) {
		if (entity == null) {
			return null;
		}
		CategoryTypeMasterVO vo = new CategoryTypeMasterVO();
		if (entity.getCatTypeId() != null) {
			vo.setCatTypeId(entity.getCatTypeId());
		}
		vo.setCatTypeName(entity.getCatTypeName());
		vo.setCreatedBy(entity.getCreatedBy());
		if (entity.getCreatedOn() != null)
			vo.setCreatedOn(toXMLGregorianCalendar(entity.getCreatedOn()));
		if (entity.getModifiedOn() != null)
			vo.setModifiedOn(toXMLGregorianCalendar(entity.getModifiedOn()));
		
		vo.setModifiedBy(entity.getModifiedBy());
		vo.setStatus(entity.getStatus());

		return vo;
	}

	public static DepartmentMasterVO convert(DepartmentMasterEntity entity) {
		if (entity == null) {
			return null;
		}
		DepartmentMasterVO vo = new DepartmentMasterVO();
		if (entity.getDeptId() != null) {
			vo.setDeptId(entity.getDeptId());
		}
		vo.setDeptName(entity.getDeptName());
		return vo;
	}

	public static DoctorMasterVO convert(DoctorMasterEntity entity) {
		if (entity == null) {
			return null;
		}
		DoctorMasterVO vo = new DoctorMasterVO();
		if (entity.getId() != null) {
			vo.setId(entity.getId());
		}
		if (entity.getCategoryType() != null)
			vo.setCategoryType(EntityToVOConverter.convert(entity
					.getCategoryType()));
		vo.setActive(entity.getActive());
		vo.setContactNo(entity.getContactNo());
		vo.setCreatedBy(entity.getCreatedBy());
		vo.setDegree(entity.getDegree());
		vo.setFirstName(entity.getFirstName());

		vo.setLastName(entity.getLastName());
		vo.setMiddleName(entity.getMiddleName());
		vo.setModifiedBy(entity.getModifiedBy());
		vo.setSpeciality(entity.getSpeciality());
		
		if (entity.getCreatedOn() != null)
			vo.setCreatedOn(toXMLGregorianCalendar(entity.getCreatedOn()));
		if (entity.getModificationOn() != null)
			vo.setModificationOn(toXMLGregorianCalendar(entity.getModificationOn()));
		
		
		DepartmentMasterEntity departmentEntity = entity.getDepartment();
		vo.setDepartment(EntityToVOConverter.convert(departmentEntity));

		return vo;
	}

	public static MedicineVO convert(MedicineEntity entity) {
		if (entity == null) {
			return null;
		}
		MedicineVO vo = new MedicineVO();
		vo.setBatchNo(entity.getBatchNo());
		vo.setCreatedBy(entity.getCreatedBy());
		if (entity.getId() != null) {
			vo.setMedicineId(entity.getId());
		}
		if (entity.getCategoryType() != null)
			vo.setCategoryType(EntityToVOConverter.convert(entity
					.getCategoryType()));
		vo.setMedicineName(entity.getMedicineName());

		if (entity.getCreatedOn() != null)
			vo.setCreatedOn(toXMLGregorianCalendar(entity.getCreatedOn()));
		if (entity.getModificationOn() != null)
			vo.setModifiedOn(toXMLGregorianCalendar(entity.getModificationOn()));
		
		vo.setModifiedBy(entity.getModifiedBy());
		vo.setPower(entity.getPower());
		vo.setRate(entity.getRate());
		vo.setStatus(entity.getStatus());
		return vo;
	}

	public static CustomerVO convert(CustomerEntity entity) {
		CustomerVO vo = new CustomerVO();
		vo.setCustomerName(entity.getCustomerName());
		vo.setId(entity.getId());

		List<PhoneNumberEntity> list = entity.getPhones();
		List<PhoneNumberVO> volist = new ArrayList<PhoneNumberVO>();
		if (CollectionUtils.isNotEmpty(list)) {
			for (PhoneNumberEntity phone : list) {
				PhoneNumberVO phonevo = convert(phone);
				volist.add(phonevo);
			}
		}
		// vo.setPhones(volist);
		return vo;
	}

	public static PhoneNumberVO convert(PhoneNumberEntity entity) {
		PhoneNumberVO vo = new PhoneNumberVO();
		vo.setId(entity.getId());
		vo.setPhNumber(entity.getPhNumber());
		/*
		 * List<CustomerEntity> list = entity.getCustomers(); List<CustomerVO>
		 * volist = new ArrayList<CustomerVO>();
		 * if(CollectionUtils.isNotEmpty(list)){ for(CustomerEntity
		 * customerEntity:list){
		 * volist.add(EntityToVOConverter.convert(customerEntity)); } }
		 * vo.setCustomers(volist);
		 */return vo;
	}

	public static EmployeeVO convert(EmployeeEntity employee) {
		EmployeeVO employeeVO = new EmployeeVO();
		employeeVO.setAge(employee.getAge());
		employeeVO.setId(employee.getId());
		employeeVO.setName(employee.getName());

		return employeeVO;
	}

	public static CountryMasterVO convert(CountryMasterEntity countryMaster) {
		CountryMasterVO vo = new CountryMasterVO();
		vo.setCountryName(countryMaster.getCountryName());
		vo.setId(countryMaster.getId());
		/*
		 * if(CollectionUtils.isNotEmpty(countryMaster.getStateMasters())){
		 * List<StateMasterVO> states = new ArrayList<StateMasterVO>();
		 * for(StateMasterEntity entity : countryMaster.getStateMasters()){
		 * states.add(EntityToVOConverter.convert(entity)); }
		 * vo.setStateMasters(states); }
		 */
		return vo;
	}

	public static StateMasterVO convert(StateMasterEntity entity) {
		StateMasterVO vo = new StateMasterVO();
		vo.setId(entity.getId());
		vo.setStateName(entity.getStateName());
		vo.setCountryMaster(EntityToVOConverter.convert(entity
				.getCountryMaster()));
		return vo;
	}

	public static PatientVO convert(PatientEntity entity) {
		PatientVO vo = new PatientVO();
		vo.setAddress(entity.getAddress());
		vo.setAge(entity.getAge());
		vo.setContactNo(entity.getContactNo());
		vo.setCountry(entity.getCountry());
		vo.setCreatedBy(entity.getCreatedBy());
		vo.setFirstName(entity.getFirstName());
		vo.setLastName(entity.getLastName());
		vo.setMaritalstatus(entity.getMaritalstatus());
		vo.setMiddleName(entity.getMiddleName());
		vo.setModifiedBy(entity.getModifiedBy());
		vo.setNationality(entity.getNationality());
		vo.setReligion(entity.getReligion());
		vo.setSex(entity.getSex());
		vo.setState(entity.getState());
		vo.setStatus(entity.getStatus());
		vo.setUhid(entity.getUhid());
		
		if (entity.getCreatedOn() != null)
			vo.setCreatedOn(toXMLGregorianCalendar(entity.getCreatedOn()));
		if (entity.getModifiedOn() != null)
			vo.setModifiedOn(toXMLGregorianCalendar(entity.getModifiedOn()));
		
		return vo;
	}

	public static EquipmentVO convert(EquipmentEntity entity) {

		EquipmentVO equipmentVO = new EquipmentVO();
		if (entity.getCategoryType() != null)
			equipmentVO.setCategoryType(EntityToVOConverter.convert(entity
					.getCategoryType()));
		equipmentVO.setCreatedBy(entity.getCreatedBy());
		equipmentVO.setEquipmentId(entity.getEquipmentId());
		equipmentVO.setEquipmentName(entity.getEquipmentName());
		equipmentVO.setModifiedBy(entity.getModifiedBy());
		
		if (entity.getCreatedOn() != null)
			equipmentVO.setCreatedOn(toXMLGregorianCalendar(entity.getCreatedOn()));
		if (entity.getModifiedOn() != null)
			equipmentVO.setModifiedOn(toXMLGregorianCalendar(entity.getModifiedOn()));
		
		
		equipmentVO.setPrice(entity.getPrice());
		equipmentVO.setStatus(entity.getStatus());
		return equipmentVO;
	}

	public static ConsumableVO convert(ConsumableEntity entity) {
		if (entity == null) {
			return null;
		}

		ConsumableVO consumableVO = new ConsumableVO();
		if (entity.getCategoryType() != null)
			consumableVO.setCategoryType(EntityToVOConverter.convert(entity
					.getCategoryType()));
		if (entity.getConsumableId() != null)
			consumableVO.setConsumableId(entity.getConsumableId());
		consumableVO.setConsumableName(entity.getConsumableName());
		consumableVO.setConsumablePrice(entity.getConsumablePrice());
		consumableVO.setCreatedBy(entity.getCreatedBy());
		consumableVO.setModifiedBy(entity.getModifiedBy());
		consumableVO.setStatus(entity.getStatus());
		if (entity.getCreatedOn() != null)
			consumableVO.setCreatedOn(toXMLGregorianCalendar(entity.getCreatedOn()));
		if (entity.getModifiedOn() != null)
			consumableVO.setModifiedOn(toXMLGregorianCalendar(entity.getModifiedOn()));
		
		return consumableVO;
	}

	public static OperatorVO convert(OperatorEntity entity) {
		if (entity == null)
			return null;

		OperatorVO vo = new OperatorVO();
		if (entity.getOperatorId() != null)
			vo.setOperatorId(entity.getOperatorId());
		vo.setOperatorName(entity.getOperatorName());
		vo.setPhoneNo(entity.getPhoneNo());
		return vo;
	}

	public static TreatmentPackageVO convert(TreatmentPackageEntity entity) {
		TreatmentPackageVO packageVO = new TreatmentPackageVO();
		packageVO.setNoOfAppointment(entity.getNoOfAppointment());
		packageVO.setTotalAmount(entity.getTotalAmount());
		if(entity.getTreatmentPackageId()!=null)
			packageVO.setTreatmentPackageId(entity.getTreatmentPackageId());
		packageVO.setTreatmentPackageName(entity.getTreatmentPackageName());
		
		return packageVO;
	}
}
